<%@ include file="../common/IncludeTop.jsp"%>

<div id="BackLink">
    <a href="main">Return to Main Menu</a>
</div>

<script src="${pageContext.request.contextPath}/js/CartAutoRefresh.js"></script>
<div id="Catalog">

    <div id="Cart">

        <h2>Shopping Cart</h2>
        <form action="updateCart">
            <table>
                <tr>
                    <th><b>Item ID</b></th>
                    <th><b>Product ID</b></th>
                    <th><b>Description</b></th>
                    <th><b>In Stock?</b></th>
                    <th><b>Quantity</b></th>
                    <th><b>List Price</b></th>
                    <th><b>Total Cost</b></th>
                    <th>&nbsp;</th>
                </tr>

                <c:if test="${sessionScope.cart.numberOfItems == 0}">
                    <tr>
                        <td colspan="8"><b>Your cart is empty.</b></td>
                    </tr>
                </c:if>

                <c:forEach var="cartItem" items="${sessionScope.cart.cartItems}">
                    <tr>
                        <td>
                            <a href="viewItem?itemId=${cartItem.item.itemId}" class="${cartItem.item.itemId}" id="${cartItem.item.itemId}" name="${cartItem.item.itemId}">
                                    ${cartItem.item.itemId}
                            </a>
                        </td>
                        <td>
                                ${cartItem.item.product.productId}
                        </td>
                        <td>
                                ${cartItem.item.attribute1} ${cartItem.item.attribute2}
                                ${cartItem.item.attribute3} ${cartItem.item.attribute4}
                                ${cartItem.item.attribute5} ${cartItem.item.product.name}
                        </td>
                        <td>
                                ${cartItem.inStock}
                        </td>
                        <td>
                            <input type="text" id="${cartItem.item.itemId}" onblur="updateCart1();" class="quantity" name="${cartItem.item.itemId}" value="${cartItem.quantity}">
                        </td>

                        <td><fmt:formatNumber value="${cartItem.item.listPrice}"
                                              pattern="$#,##0.00" />
                        </td>
                        <td>
                            <label id="total" class="${cartItem.item.itemId}">${cartItem.total}</label>
                        <!--    <fmt:formatNumber value="${cartItem.total}"
                                              pattern="$#,##0.00" />
                                              !-->
                        </td>
                        <td>
                            <a class="Button" href="removeItemFromCart?workingItemId=${cartItem.item.itemId}">Remove</a>
                        </td>

                    </tr>
                </c:forEach>
                <tr>
                    <td colspan="7">
                        Sub Total:<label id="subtotal">${sessionScope.cart.subTotal}</label>
                       <!-- <fmt:formatNumber
                            value="${sessionScope.cart.subTotal}" pattern="$#,##0.00" />
                        !-->
                        <input type="submit" name="updateCart" value="Update Cart"/>

                    </td>
                    <td>&nbsp;</td>
                </tr>
            </table>

        </form>
        <c:if test="${sessionScope.cart.numberOfItems > 0}">
            <a href="newOrderForm" class="Button">
                Proceed to checkout
            </a>
        </c:if>
    </div>

    <div id="Separator">&nbsp;</div>
</div>
<%--<script type="text/javascript">--%>
<%--    var xhr;--%>
<%--    var ids = new Array();--%>
<%--    function updateCart1(){--%>

<%--        var len = document.getElementsByClassName("quantity").length;--%>
<%--        var arr = new Array();--%>

<%--        var amounts = new Array();--%>
<%--        for(var i=0;i<len;i++){--%>
<%--            arr[i] = document.getElementsByClassName("quantity").item(i);--%>
<%--            ids[i] = arr[i].id;--%>
<%--            amounts[i] = arr[i].value;--%>
<%--            // console.log(arr[i].value);--%>
<%--            // console.log(arr[i].id);--%>
<%--        }--%>

<%--        //console.log(arr);--%>
<%--        xhr=new XMLHttpRequest();--%>
<%--        xhr.onreadystatechange=process;--%>
<%--        xhr.open("GET","cartAutoRefresh?ids="+ids+"&amounts="+amounts,true);--%>
<%--        xhr.send(null);--%>
<%--    }--%>
<%--    function process(){--%>
<%--        if(xhr.readyState==4){--%>
<%--            if(xhr.status==200){--%>
<%--                var responseText = xhr.responseText;--%>
<%--                //console.log(responseText);--%>
<%--                var array = responseText.split(",");--%>

<%--                var len = document.getElementsByClassName("quantity").length;--%>
<%--                var arr = new Array();--%>
<%--                var quantities = new Array();--%>
<%--                var total=new Array();--%>

<%--                for(var i=0;i<len;i++){--%>
<%--                   // arr[i] = document.getElementsByClassName("quantity").item(i);--%>
<%--                    quantities[i] = document.getElementsByClassName("quantity");--%>
<%--                    total[i] = document.getElementsByClassName(ids[i]);--%>
<%--                    console.log(total[i]);--%>
<%--                }--%>

<%--                for(var i=0,j=0;i<len*2;i=i+2,j++){--%>
<%--                        quantities[j].innerText=array[i];--%>
<%--                        total[j][1].innerText = array[i+1];--%>
<%--                        console.log(total[j].innerText);--%>
<%--                }--%>


<%--                // var quantity = document.getElementsByClassName("quantity");--%>
<%--                // var total = document.getElementById("total");--%>
<%--                var subtotal = document.getElementById("subtotal");--%>

<%--                // quantity.innerText = array[0];--%>
<%--                // total.innerText = array[1];--%>
<%--                subtotal.innerText = array[array.length-1];--%>
<%--            }--%>
<%--        }--%>
<%--    }--%>

<%--</script>--%>

<%@ include file="../common/IncludeBottom.jsp"%>